//
//  ArticleManager.m
//  Showing
//
//  Created by Ralf on 3/31/15.
//  Copyright (c) 2015 next-software. All rights reserved.
//

@import UIKit;

#import "ArticleManager.h"
#import "AppDelegate.h"
#import "AttachmentManager.h"

@implementation ArticleManager

- (ResultSet *)createArticle:(NSDictionary *)article
{

    //    if(image == nil || image.size.width < 180 || image.size.height < 180)
    //    {
    //        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"错误"
    //                                                        message:@"请选择一张图片"
    //                                                       delegate:nil
    //                                              cancelButtonTitle:@"确定"
    //                                              otherButtonTitles:nil];
    //        [alert show];
    //        return @"400";
    //    }

    //    if(message == nil || message.length < 10)
    //    {
    //        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"错误"
    //                                                        message:@"请输入10个字"
    //                                                       delegate:nil
    //                                              cancelButtonTitle:@"确定"
    //                                              otherButtonTitles:nil];
    //        [alert show];
    //        return @"400";
    //    }
    //
    //    if(message.length > 200)
    //    {
    //        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"错误"
    //                                                        message:@"您输入的字数不能多于200"
    //                                                       delegate:nil
    //                                              cancelButtonTitle:@"确定"
    //                                              otherButtonTitles:nil];
    //        [alert show];
    //        return @"400";
    //    }

    NSMutableDictionary *params = [NSMutableDictionary new];

    if (article[@"indexOfSaleTags"])
    {
        params[@"indexOfSaleTags"] = article[@"indexOfSaleTags"];
    }

    if (article[@"indexOfTags"])
    {
        params[@"indexOfTags"] = article[@"indexOfTags"];
    }

    params[@"message"] = article[@"message"];

    if (article[@"address"])
    {
        params[@"address"] = article[@"address"];
    }

    params[@"sessionID"] = [self getSessionID];

    //    NSData *dImage = UIImageJPEGRepresentation(article[@"image"], 0.6);
    //    NSData *data = [dImage base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
    //    NSString *sImage = [[NSString alloc] initWithBytes:[data bytes] length:data.length encoding:NSUTF8StringEncoding];
    //    params[@"image"] = sImage;

    NSString *mediaType = article[@"mediaType"];

    if ([mediaType isEqualToString:@"Image"])
    {
        //        mediaType = @"Image";
        NSData   *dImage = UIImageJPEGRepresentation(article[@"image"], 0.6);
        NSData   *data   = [dImage base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
        NSString *sImage = [[NSString alloc] initWithBytes:[data bytes] length:data.length encoding:NSUTF8StringEncoding];
        params[@"image"] = sImage;
    }

    [params setValue:mediaType forKey:@"mediaType"];

    if ([mediaType isEqualToString:@"Video"])
    {
        NSData   *dImage = UIImageJPEGRepresentation(article[@"image"], 0.6);
        NSData   *data   = [dImage base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
        NSString *sImage = [[NSString alloc] initWithBytes:[data bytes] length:data.length encoding:NSUTF8StringEncoding];

        params[@"image"] = sImage;
        NSData *video = article[@"video"];
        data = [video base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
        NSString *string = [[NSString alloc] initWithBytes:[data bytes] length:data.length encoding:NSUTF8StringEncoding];
        params[@"video"] = string;

    }

    ResultSet *rs = [self requestJSON:@"article/createArticle" params:params];

    if (rs != nil && ![rs hasErrors])
    {
        NSMutableArray *images = article[@"images"];
        if (images.count > 0)
        {
            NSDictionary      *article           = [rs toDictionary];
            NSString          *articleId         = article[@"id"];
            AttachmentManager *attachmentManager = [AttachmentManager new];
            for (NSDictionary      *image in images)
            {
                [attachmentManager uploadImage:articleId image:image[@"image"] desc:image[@"description"]];
            }
        }
    }

    return rs;
}

- (ResultSet *)queryByUserName:(NSString *)userName max:(NSInteger)max offset:(NSInteger)offset
{
    NSMutableDictionary *params = [NSMutableDictionary new];

    params[@"userName"] = userName;
    NSString *sessionID = [self getSessionID];
    if (sessionID && sessionID.length > 0)
    {
        params[@"sessionID"] = [self getSessionID];
    }
    params[@"max"]      = [[NSString alloc] initWithFormat:@"%ld", (long) max];
    params[@"offset"]   = [[NSString alloc] initWithFormat:@"%ld", (long) offset];

    return [self request:@"article/queryByUserName" params:params];
}

- (ResultSet *)queryMyArticle:(NSInteger)max offset:(NSInteger)offset
{
    NSMutableDictionary *params = [NSMutableDictionary new];
    params[@"sessionID"] = [self getSessionID];
    params[@"max"]       = [[NSString alloc] initWithFormat:@"%ld", (long) max];
    params[@"offset"]    = [[NSString alloc] initWithFormat:@"%ld", (long) offset];
    return [self request:@"article/queryMyArticle" params:params];
}

- (ResultSet *)queryMyAppointment:(NSInteger)max offset:(NSInteger)offset reviewed:(BOOL)reviewed
{
    NSMutableDictionary *params = [NSMutableDictionary new];
    params[@"sessionID"] = [self getSessionID];
    params[@"max"]       = [[NSString alloc] initWithFormat:@"%ld", (long) max];
    params[@"offset"]    = [[NSString alloc] initWithFormat:@"%ld", (long) offset];
    params[@"reviewed"]  = [[NSString alloc] initWithFormat:@"%ld", (long) reviewed];
    return [self request:@"article/queryMyAppointment" params:params];
}

- (ResultSet *)queryMyOrder:(NSInteger)max offset:(NSInteger)offset
{
    NSMutableDictionary *params = [NSMutableDictionary new];
    params[@"sessionID"] = [self getSessionID];
    params[@"max"]       = [[NSString alloc] initWithFormat:@"%ld", (long) max];
    params[@"offset"]    = [[NSString alloc] initWithFormat:@"%ld", (long) offset];
    return [self request:@"article/queryMyOrder" params:params];
}

- (ResultSet *)queryMyTask:(NSInteger)max offset:(NSInteger)offset
{
    NSMutableDictionary *params = [NSMutableDictionary new];
    params[@"sessionID"] = [self getSessionID];
    params[@"max"]       = [[NSString alloc] initWithFormat:@"%ld", (long) max];
    params[@"offset"]    = [[NSString alloc] initWithFormat:@"%ld", (long) offset];
    return [self request:@"article/queryMyTask" params:params];
}

- (ResultSet *)queryMyAdvertising:(NSInteger)max offset:(NSInteger)offset status:(NSString *)status//@"Ready for Sale", @"Disable"
{
    NSMutableDictionary *params = [NSMutableDictionary new];
    params[@"sessionID"] = [self getSessionID];
    params[@"max"]       = [[NSString alloc] initWithFormat:@"%ld", (long) max];
    params[@"offset"]    = [[NSString alloc] initWithFormat:@"%ld", (long) offset];
    if (status)
    {
        params[@"status"] = status;
    }
    return [self request:@"article/queryMyAdvertising" params:params];
}

- (ResultSet *)searchByMessage:(NSString *)message max:(NSInteger)max offset:(NSInteger)offset
{
    NSMutableDictionary *params = [NSMutableDictionary new];

    AppDelegate *appDelegate = [self getAppDelegate];
    if ([appDelegate isLogged])
    {
        params[@"sessionID"] = [self getSessionID];
    }
    params[@"max"]           = [[NSString alloc] initWithFormat:@"%ld", (long) max];
    params[@"offset"]        = [[NSString alloc] initWithFormat:@"%ld", (long) offset];
    params[@"message"]       = message;
    return [self request:@"article/searchByMessage" params:params];
}

- (ResultSet *)queryArticle:(NSInteger)max offset:(NSInteger)offset
{
    NSMutableDictionary *params      = [NSMutableDictionary new];
    AppDelegate         *appDelegate = [self getAppDelegate];
    if ([appDelegate isLogged])
    {
        params[@"sessionID"] = [self getSessionID];
    }
    params[@"max"]                   = [[NSString alloc] initWithFormat:@"%ld", (long) max];
    params[@"offset"]                = [[NSString alloc] initWithFormat:@"%ld", (long) offset];
    return [self request:@"article/queryArticle" params:params];
}

@end
